package com.wc.dao.impl;

import com.wc.dao.AdminDao;
import com.wc.domain.Admin;
import com.wc.utils.CyptoUtil;
import com.wc.utils.JdbcUtil;
import com.wc.utils.RandomUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AdminDaoImpl implements AdminDao {

    @Override
    public Admin selectById(Integer id) throws SQLException {
        Admin admin = null;
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from tb_admin where id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String nickname = rs.getString("nickname");
            String account = rs.getString("account");
            String password = rs.getString("password");
            String avatar = rs.getString("avatar");
            String salt = rs.getString("salt");
            admin = new Admin(id, nickname, account, password,avatar, salt);
        }
        JdbcUtil.release(conn, ps, rs);
        return admin;
    }

    @Override
    public Admin selectByAccount(String account) throws SQLException {
        Admin admin = null;
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from tb_admin where account = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, account);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            String nickname = rs.getString("nickname");
            String password = rs.getString("password");
            String avatar = rs.getString("avatar");
            String salt = rs.getString("salt");
            admin = new Admin(id, nickname, account, password, avatar,salt);
        }
        JdbcUtil.release(conn, ps, rs);
        return admin;
    }

    @Override
    public boolean updatePassword(Integer id, String password) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql = "update tb_admin set password = ? where id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, password);
        ps.setInt(2, id);
        int res = ps.executeUpdate();
        JdbcUtil.release(conn, ps, null);
        return res == 1 ? true : false;
    }

    @Override
    public boolean updateSalt(Integer id, String salt) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql = "update tb_admin set salt = ? where id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, salt);
        ps.setInt(2, id);
        int res = ps.executeUpdate();
        JdbcUtil.release(conn, ps, null);
        return res == 1 ? true : false;
    }

    @Override
    public int updateAllSaltAndPassword() throws SQLException {
        int res = 0;
        Connection conn = JdbcUtil.getConnection();

        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from tb_admin");
        while (rs.next()){
            int id = rs.getInt("id");
            String salt = rs.getString("salt");
            String password = rs.getString("password");

            String sql = "update tb_admin set salt = ? , password = ? where id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);

            String newSalt = RandomUtil.genString(5, 16);
            ps.setString(1,newSalt);

            String oldPassword = CyptoUtil.decode(password, salt);
            String newPassword = CyptoUtil.encode(oldPassword, newSalt);
            ps.setString(2,newPassword);

            ps.setInt(3,id);

            ps.executeUpdate();
            res ++;

            if(ps != null){
                ps.close();
            }
        }

        JdbcUtil.release(conn, stmt, rs);
        System.out.println("*******");
        return  res;
    }
}
